package service;

import model.User;
import java.util.HashMap;
import java.util.Map;

public class AuthService {
    // 1. 添加final修饰符
    private final Map<String, User> users;
    private static AuthService instance;

    private AuthService() {
        // 2. 初始化users
        users = new HashMap<>();
        // 初始化测试用户
        users.put("user1", new User("user1", "123456", false));
        users.put("admin1", new User("admin1", "admin123", true));
    }

    // 3. 修正getInstance方法
    public static synchronized AuthService getInstance() {
        if (instance == null) {
            instance = new AuthService();
        }
        return instance;
    }

    // 4. 修正authenticate方法
    public User authenticate(String username, String password, boolean isAdmin) {
        User user = users.get(username);
        // 5. 添加null检查
        if (user != null && user.getPassword().equals(password)) {  // 修正括号
            if (user.isAdmin() == isAdmin) {
                return user;
            }
        }
        return null;
    }
}